package com.hyc.myvideo.ui.activity.my;

import android.content.Intent;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.hyc.myvideo.R;
import com.hyc.myvideo.data.adapter.my.course.CourseDetailsAdapter;
import com.hyc.myvideo.data.api.model.BaseResult;
import com.hyc.myvideo.data.api.model.ModelCourseDetails;
import com.hyc.myvideo.ui.base.BaseFActivity;
import com.hyc.myvideo.ui.view.DialogApproval;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import timber.log.Timber;

;

/**
 * 我的课程详情列表
 * @author gyb
 * @version 1.0
 * @created 2017/07/21
 */
public class CourseDetailsActivity extends BaseFActivity{
    @BindView(R.id.common_head_list_id)
    PullToRefreshListView toRefreshListView;
    /**页数*/
    private int page = 1;
    /**是否刷新*/
    private boolean isRefresh = true;
    private List<ModelCourseDetails> listData = null;
    private CourseDetailsAdapter adapter = null;
    /**课程ID*/
    private  String id = "";
    /***/
    private int type = -1;
    private  ViewHolder viewHolder = null;
    /**添加头部视图*/
    private  View headerView= null;
    private DialogApproval approval= null;
    @Override
    public int getContentViewId() {
        return R.layout.activity_common_listview_head;
    }

    @Override
    protected void initView() {
        try {
            setLeftBtnClick(true, "课程内容");
            Intent intent =  getIntent();
            id = intent.getStringExtra("b_id");
            type = intent.getIntExtra("type",-1);
        }catch (Exception e){
            Timber.e(e,"课程详情列表异常");
        }

        headerView = View.inflate(this, R.layout.activity_adapter_coursedetails_header,null);
        viewHolder = new ViewHolder(headerView);
        listData = new ArrayList<>();
        adapter = new CourseDetailsAdapter(this,listData,callback);
        toRefreshListView.setAdapter(adapter);
        toRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                requestData(true);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                requestData(false);
            }
        });
    }

    /***
     *事件回调
     */
    CourseDetailsAdapter.Callback callback = new CourseDetailsAdapter.Callback() {
        @Override
        public void onClickApproval(View view) {
            final ModelCourseDetails   courseDetails = (ModelCourseDetails) view.getTag();
            approval = new DialogApproval(new DialogApproval.SubmitInterface() {
                @Override
                public void onSubmit(String sr) {
                    Timber.i(courseDetails+"=======审批提交==================="+sr);

                }
            });
            approval.show(getSupportFragmentManager(),"审批");
        }
    };


    @Override
    protected void initData(){
        requestData(true);
    }

    private void requestData(boolean isRefresh){
        this.isRefresh = isRefresh;
        if(isRefresh){
            page = 1;
        }else{
            page++;
        }
        api.requestDirectHourData(page,id);
    }


    @Override
    protected void oNext(String r, String method) {
        toRefreshListView.getRefreshableView().removeHeaderView(headerView);
        BaseResult<List<ModelCourseDetails>> data = JSONObject.parseObject(r, new TypeReference<BaseResult<List<ModelCourseDetails>>>(){});
        if(data.isCode()){
            if(isRefresh){
                listData.clear();
            }
            addListViewHeader();
            listData.addAll(data.getResult());
            adapter.updateListView(listData);
        }
        clearedListView();
    }

    @Override
    protected void oError() {
        clearedListView();
    }

    /***
     * 关闭刷新
     */
    private void clearedListView(){
        if(toRefreshListView.isRefreshing()){
            toRefreshListView.onRefreshComplete();
        }
    }


    public  class ViewHolder {
        public ViewHolder() {
        }
        public ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
        /**课程标题*/
        @BindView(R.id.details_title)
        TextView textViewTitle;

    }

    private void addListViewHeader(){
        toRefreshListView.getRefreshableView().addHeaderView(headerView);
        switch (type){
            case 0:
                viewHolder.textViewTitle.setText("课时列表");
                break;
            case 1:
                viewHolder.textViewTitle.setText("已完成课程");
                break;
        }
    }


}
